<!doctype html>
<html class="no-js">

<head>
    <title>添加库存</title>
    <#include "/boya/template/meta.html" />
</head>

<body>
    <div id="app" class="page">
        <!-- <form class="layui-form" action="">
            <div class="layui-form-item">
                <label class="layui-form-label">食物分类</label>
                <div class="layui-input-block">
                    <select lay-verify="" lay-search v-model="categoryId">
                        <option value="" disabled></option>
                        <option :value="item.attrs.id" v-for="(item, index) in categorySelection">{{ item.attrs.parentName }} / {{ item.attrs.name }}</option>
                    </select>
                </div>
                <div class="layui-input-block">
                    <input type="text" required lay-verify="" placeholder="请输入食物分类" autocomplete="off" class="layui-input" v-model="categoryString">
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">食物</label>
                <div class="layui-input-block">
                    <input type="text" required lay-verify="" placeholder="请输入食物名称" autocomplete="off" class="layui-input" v-model="name">
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">重量(kg)</label>
                <div class="layui-input-block">
                    <input type="number" required lay-verify="" placeholder="请输入重量" autocomplete="off" class="layui-input" v-model="weight">
                </div>
            </div>
        </form>
        <div style="margin-left: 110px;">
            <button class="am-btn am-btn-primary" @click="submit">提交</button>
        </div> -->
        <el-form ref="form" label-width="80px">
            <el-form-item label="分类">
                <el-input v-model="categoryString"></el-input>
            </el-form-item>
            <el-form-item label="食物">
                <el-input v-model="name"></el-input>
            </el-form-item>
            <el-form-item label="重量">
                <el-input v-model="weight"></el-input>
            </el-form-item>
            <el-form-item label="规格">
                <el-input v-model="unit"></el-input>
            </el-form-item>
            <el-form-item>
                <el-button type="primary" @click="submit">提交</el-button>
            </el-form-item>
        </el-form>
    </div>
    <#include "/boya/template/script.html" />
    <script type="text/javascript">
    var app = new Vue({
        el: '#app',
        data() {
            return {
                pageIndex: parent.layer.getFrameIndex(window.name),
                categorySelection: [],
                id: '',
                categoryId: '',
                categoryString: '',
                name: '',
                weight: '',
                unit: ''
            }
        },
        mounted: function() {
            var self = this;
            layui.use('form', function() {
                // layui.form.on('select', function(data) {
                //     self.categoryId = data.value;
                // });
            });
            var jsonStr = '${info!}';
            console.log(jsonStr)
            if (jsonStr != '') {
                var info = ${info!}
                this.id = info.id;
                this.categoryId = info.categoryId;
                this.categoryString = info.categoryString;
                this.name = info.name;
                // this.weight = accMul(info.weight, 0.001);
                this.weight = info.weight;
                this.unit = info.unit;
            }
            var catJsonStr = '${categories!}';
            if (catJsonStr != '') {
                this.categorySelection = JSON.parse(catJsonStr);
            }
        },
        methods: {
            submit() {
                var self = this;
                var _parent = parent;
                // if (self.categoryId == '') {
                //     msg("请选择分类");
                //     return;
                // }
                if (self.categoryString == '') {
                    msg("请输入分类");
                    return;
                }
                if (self.name == '') {
                    msg("请输入名称");
                    return;
                }
                if (self.weight == '') {
                    msg("请输入重量");
                    return;
                }
                if (Number(self.weight) < 0) {
                    msg("重量不能为负数");
                    return;
                }
                if (self.unit == '') {
                    msg("请输入规格");
                    return;
                }
                var map = {
                    categoryId: self.categoryId,
                    categoryString: self.categoryString,
                    name: self.name,
                    // weight: accMul(self.weight, 1000)
                    weight: self.weight,
                    unit: self.unit
                }
                if (self.id != '') {
                    map['id'] = self.id;
                }
                var param = Qs.stringify(map);
                axios({
                        url: '/boya/submitStorage',
                        method: 'post',
                        data: param,
                        headers: {
                            'Content-Type': 'application/x-www-form-urlencoded'
                        }
                    })
                    .then(function(response) {
                        console.log(response);
                        if (response.data.status == 200) {
                            // _parent.msg("保存成功");
                            // _parent.layer.close(self.pageIndex);
                            _parent.window.location.reload();
                        } else {
                            _parent.msg("保存失败");
                            _parent.layer.close(self.pageIndex);
                            console.log(response.data);
                        }
                    })
                    .catch(function(error) {
                        _parent.msg("保存失败");
                        _parent.layer.close(self.pageIndex);
                        console.log(error);
                    });
            }
        }
    })
    </script>
    <style type="text/css">
    .page {
        padding: 15px;
    }

    .layui-form-label {
        width: 90px;
    }

    .submit-wrap {
        text-align: center;
    }
    </style>
</body>

</html>